﻿<ComClass(ClsFieldExpressionEx.ClassId, ClsFieldExpressionEx.InterfaceId, ClsFieldExpressionEx.EventsId)>
Public Class ClsFieldExpressionEx

#Region "COM GUID"
    ' 这些 GUID 提供此类的 COM 标识 
    ' 及其 COM 接口。若更改它们，则现有的
    ' 客户端将不再能访问此类。
    Public Const ClassId As String = "fb27d714-8b0c-4f8f-8e32-1248189f5e70"
    Public Const InterfaceId As String = "137b26a1-8296-465d-8f3a-28c41ec89314"
    Public Const EventsId As String = "5774f69c-551e-4a62-b21a-543e42ebf591"
#End Region

    ' 可创建的 COM 类必须具有一个不带参数的 Public Sub New() 
    ' 否则， 将不会在 
    ' COM 注册表中注册此类，且无法通过
    ' CreateObject 创建此类。
    Public Sub New()
        MyBase.New()
    End Sub

    Public Function LengthExperssion(ent As Object, 小数点位数 As Integer) As String
        If TypeName(ent) = $"IAcadLine" Or TypeName(ent) = $"IAcadLWPolyline" Then
            LengthExperssion = "%<\AcObjProp Object(%<\_ObjId " & ent.ObjectID & ">%).Length \f " & Chr(34) & "%lu2%pr" & 小数点位数 & Chr(34) & ">%"
        Else : LengthExperssion = vbNullString
        End If
    End Function

    Public Function CircleCircumferenceExperssion(ent As Object, 小数点位数 As Integer) As String
        If TypeName(ent) = "IAcadCircle" Then
            CircleCircumferenceExperssion = "%<\AcObjProp Object(%<\_ObjId " & ent.ObjectID & ">%).Circumference \f " & Chr(34) & "%lu2%pr" & 小数点位数 & Chr(34) & ">%"
        Else : CircleCircumferenceExperssion = vbNullString
        End If
    End Function

    Public Function ArcLengthExperssion(ent As Object, 小数点位数 As Integer) As String
        If TypeName(ent) = "IAcadArc" Then
            ArcLengthExperssion = "%<\AcObjProp Object(%<\_ObjId " & ent.ObjectID & ">%).ArcLength \f " & Chr(34) & "%lu2%pr" & 小数点位数 & Chr(34) & ">%"
        Else : ArcLengthExperssion = vbNullString
        End If
    End Function

    Public Function AreaExperssion(ent As Object, 小数点位数 As Integer) As String
        If TypeName(ent) = "IAcadCircle" Or TypeName(ent) = $"IAcadLWPolyline" Then
            AreaExperssion = "%<\AcObjProp Object(%<\_ObjId " & ent.ObjectID & ">%).Area \f " & Chr(34) & "%lu2%pr" & 小数点位数 & Chr(34) & ">%"
        Else : AreaExperssion = vbNullString
        End If
    End Function

    Public Function TextString(ent As Object) As String
        If TypeName(ent) = "IAcadText" Or TypeName(ent) = "IAcadMText" Or TypeName(ent) = "IAcadAttributeReference" Then
            TextString = "%<\AcObjProp Object(%<\_ObjId " & ent.ObjectID & ">%).TextString>%"
        Else : TextString = vbNullString
        End If
    End Function

    Public Function BlkrefName(blkref As Object) As String
        BlkrefName = "%<\AcObjProp Object(%<\_ObjId " & blkref.ObjectID & ">%).EffectiveName >%"
    End Function


    Public Function BlkrefAttValue(blkref As Object, attTag As String) As String
        Dim rtn As String = vbNullString
        If blkref.HasAttributes Then
            Dim atts As Object, attref As Object
            atts = blkref.GetAttributes()
            For Each attref In atts
                If attref.TagString = attTag Then
                    rtn = TextString(attref)
                    Exit For
                End If
            Next
        Else : rtn = vbNullString
        End If
        Return rtn
    End Function

    ''' <summary>
    ''' 获取图纸的属性
    ''' </summary>
    ''' <param name="doc"></param>
    ''' <param name="dwgProprtyName"></param>
    ''' <returns></returns>
    Public Function GetDwgPropertyValue(doc As Object, dwgProprtyName As String) As String
        If doc.SummaryInfo.NumCustomInfo() > 0 Then
            Dim dwgInfor As Object, tempStr As String = vbNullString
            dwgInfor = doc.SummaryInfo
            dwgInfor.GetCustomByKey(dwgProprtyName, tempStr)
            If tempStr <> vbNullString Then
                GetDwgPropertyValue = "%<\AcVar CustomDP." & dwgProprtyName & "%"
            Else
                GetDwgPropertyValue = vbNullString
            End If
        Else
            GetDwgPropertyValue = vbNullString
        End If
    End Function

End Class


